package com.hdl.school.utils;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.leectrl.utils.HexUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.nio.charset.StandardCharsets;

public class RecorderApi {

    /**
     * 查询录播状态
     * @param config
     * @param onRecorderStatusReturn
     */
    public static void queryRecorderStatus(JSONObject config,OnRecorderStatusReturn onRecorderStatusReturn){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=24673&user=%s&password=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase());
            try {
                x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if(onRecorderStatusReturn!=null){
                            String[] recorderInfo=result.split("\r\n");
                            if(recorderInfo.length>=4){
                                int[] res=new int[]{0,0,0};
                                for(int j=0;j<recorderInfo.length;j++){
                                    String item=recorderInfo[j];
                                    if(item.startsWith("VOBActive")){
                                        res[1]=Integer.parseInt(item.substring(10,11));
                                    }else if(item.startsWith("SysStatus")){
                                        res[0]=Integer.parseInt(item.substring(10,11))==1?0:1;
                                    }else if(item.startsWith("RecActive")){
                                        res[2]=Integer.parseInt(item.substring(10,11));
                                    }
                                }
                                onRecorderStatusReturn.onGotRecorderStatus(res);
                            }else{
                                onRecorderStatusReturn.onGotRecorderStatus(null);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        if(onRecorderStatusReturn!=null){
                            onRecorderStatusReturn.onGotRecorderStatus(null);
                        }

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 开始录制
     * @param config
     */
    public static void startRecord(JSONObject config){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( "record_setParam_enable=1".getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            Log.e("tcp",url);
            x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Log.e("tcp","startRecord:"+result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

    }

    /**
     * 暂停录制
     * @param config
     */
    public static void pauseRecord(JSONObject config){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( "record_setParam_enable=2".getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Log.e("tcp","pauseRecord:"+result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }

    /**
     * 暂停录制
     * @param config
     */
    public static void continueRecord(JSONObject config){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( "record_setParam_enable=3".getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Log.e("tcp","continueRecord:"+result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }


    /**
     * 停止录制
     * @param config
     */
    public static void stopRecord(JSONObject config){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( "record_setParam_enable=0".getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            Log.e("tcp",url);
            x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Log.e("tcp","stopRecord:"+result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }



    public static void recorderPowerSwitch(JSONObject config,boolean isOn){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( ("machine_control_param="+(isOn?"reboot":"sleep")).getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            x.http().get(new RequestParams(url),null);
        }
    }

    public static void livingSwitch(JSONObject config,boolean isOn){
        String url=null;
        String brand=config.getString("brand");
        if("ava".equals(brand)){
            String data= HexUtil.bytes2HexString( ("live_setParam_enable="+(isOn?"1":"0")).getBytes(StandardCharsets.UTF_8)).toLowerCase();
            url=String.format("http://%s/cgi-bin/plat/plat.cgi?action=9&user=%s&pwsd=%s&command=1&data=%s",config.getString("host"),config.getString("username"),SignUtil.md5(config.getString("password")).toLowerCase(),data);
            x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Log.e("tcp","livingSwitch:"+isOn+","+result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }



    interface  OnRecorderStatusReturn{
       public void  onGotRecorderStatus(int[] status);
    }

}
